const cloud = require('wx-server-sdk')

cloud.init({
  env: cloud.DYNAMIC_CURRENT_ENV
})

const db = cloud.database()
const _ = db.command

exports.main = async (event, context) => {
  const { roomId, updatePainterIndex } = event.data
  let data = {
    updateTime: new Date()
  }
  Object.keys(event.data).forEach(key => {
    if (event.data[key]) {
      data[key] = event.data[key]
    }
  })
  delete data.roomId
  if (updatePainterIndex) {
    data = {
      painterIndex: _.inc(1),
      updateTime: new Date()
    }
  }
  return db.collection('room').doc(roomId).update({
    data
  })
}